#include <iostream>
#include <string>
#include <cstring>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include "protocol.hpp"

using namespace std;

int main(int argc,char* argv[])
{
  if(argc!=3)
  {
    cout<<argv[0]<<" ip port"<<endl;
    exit(1);
  }

  int sock=socket(AF_INET,SOCK_STREAM,0);
  if(sock<0)
  {
    cout<<" socket error"<<endl;
    exit(2);
  }

  struct sockaddr_in peer;
  memset(&peer,0,sizeof(peer));
  peer.sin_family=AF_INET;
  peer.sin_port=htons(atoi(argv[2]));
  peer.sin_addr.s_addr=inet_addr(argv[1]);
  socklen_t len=sizeof(peer);

  if(connect(sock,(struct sockaddr*)&peer,len)<0)
  {
    cout<<"connect error"<<endl;
    exit(3);
  }

  while(true)
  {
    request rq;
        cout << "输入第一个数据# ";
        cin >> rq.x;
        cout << "输入第二个数据# ";
        cin >> rq.y;
        cout << "输入你的操作[+-*/%]# ";
        cin >> rq.op;

        send(sock, &rq, sizeof(rq), 0); //结构化的数据，你怎么没有序列化呢?弱化
        response rsp;
        recv(sock, &rsp, sizeof(rsp), 0);

        cout <<"status: " << rsp.cond << endl;
        cout << rq.x << rq.op << rq.y << "=" << rsp.result << endl;
  }
  return 0;
}

